/*
 * DaoBase.java
 *
 * Created on 18 gennaio 2006, 13.41
 */

package it.azienda.framework.persistenza;

import it.azienda.framework.modello.*;
import org.hibernate.*;
import java.util.*;

public class DaoBase {
    
    /** Creates a new instance of DaoBase */
    public DaoBase() {
    }
    
    public boolean modifica(BeanBase bean) throws Exception{
        boolean modificato = false;
        Transaction tr = null;
        Session session = DAOConnectionSingleton.newSession();
        try{
            DAOConnectionSingleton.beginTransaction();
            session.update(bean);
            DAOConnectionSingleton.commitTransaction();
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            modificato = false;
            try{
                DAOConnectionSingleton.rollbackTransaction();
            }catch(Exception exc){}
            throw ex;
        } finally{
         //   DAOConnectionSingleton.closeSession();
            modificato = true;
        }
        return modificato;
    }
    public boolean inserisci(BeanBase bean) throws Exception{
        boolean inserito = false;
        Transaction tr = null;
        Session session = DAOConnectionSingleton.newSession();
        try{
            DAOConnectionSingleton.beginTransaction();
            session.save(bean);
            DAOConnectionSingleton.commitTransaction();
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            inserito = false;
            
            try{
                DAOConnectionSingleton.rollbackTransaction();
            }catch(Exception exc){
                
            }
          throw ex;
          
        } finally{
          //  DAOConnectionSingleton.closeSession();
            inserito = true;
        }
        return inserito;
    }
    
    public boolean cancella(BeanBase bean) throws Exception{
        boolean cancellato = false;
        Session session = DAOConnectionSingleton.newSession();
        Transaction tr = null;
        try{
            DAOConnectionSingleton.beginTransaction();
            session.delete(bean);
            DAOConnectionSingleton.commitTransaction();
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            cancellato = false;
            try{
                DAOConnectionSingleton.rollbackTransaction();
            }catch(Exception exc){}
            throw ex;
        } finally{
         //   DAOConnectionSingleton.closeSession();
            cancellato = true;
        }
        return cancellato;
        
    }
    public ArrayList trovaTutti(String query) throws DAOException{
        // Query Example: "from tw_bilancio in class BeanBilancio"
        ArrayList elementi = new ArrayList();
        Session session = DAOConnectionSingleton.newSession();
        try{
            DAOConnectionSingleton.beginTransaction();
            Query q = session.createQuery(query);
            elementi = (ArrayList)q.list();
            DAOConnectionSingleton.commitTransaction();
        } catch(HibernateException e) {
            System.out.println("Errore nella query: "+e);
        } catch(Exception ex){
            
            try{DAOConnectionSingleton.rollbackTransaction();}catch(Exception exc){}
            throw new DAOException(ex.toString());
        } finally{
            DAOConnectionSingleton.closeSession();
        }
        return elementi;
    }
    
    public Object trovaElementoById(Integer id, BeanBase bean) throws DAOException{
        Session session = DAOConnectionSingleton.newSession();
        Object objReturn = null;
        try{
            DAOConnectionSingleton.beginTransaction();
            objReturn = (Object)session.get(bean.getClass(), id);
            DAOConnectionSingleton.commitTransaction();
        }catch(Exception ex){
            System.out.println("Errore nella load: "+ex);
            try{DAOConnectionSingleton.rollbackTransaction();}catch(Exception exc){}
            throw new DAOException(ex.toString());
        } finally{
            DAOConnectionSingleton.closeSession();
        }
        return objReturn;
    }
    
    
    public boolean modificaInTransaction(BeanBase bean) throws Exception{
        boolean modificato = false;
        Transaction tr = null;
        Session session = DAOConnectionSingleton.newSession();
        try{
            session.update(bean);
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            modificato = false;
            throw ex;
        } 
        return modificato;
    }
    public Integer inserisciInTransaction(BeanBase bean) throws Exception{
        boolean inserito = false;
        Transaction tr = null;
        Integer id = null;
        Session session = DAOConnectionSingleton.newSession();
        try{
            id = (Integer)session.save(bean);
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            inserito = false;
            throw ex;
        } 
        return id;
    }
    
    public boolean cancellaInTransaction(BeanBase bean) throws Exception{
        boolean cancellato = false;
        Session session = DAOConnectionSingleton.newSession();
        Transaction tr = null;
        try{
            session.delete(bean);
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            cancellato = false;
            throw ex;
        } 
        return cancellato;
        
    }
    public ArrayList trovaTuttiInTransaction(String query) throws DAOException{
        // Query Example: "from tw_bilancio in class BeanBilancio"
        ArrayList elementi = new ArrayList();
        Session session = DAOConnectionSingleton.newSession();
        try{
            Query q = session.createQuery(query);
            elementi = (ArrayList)q.list();
        }catch(Exception ex){
            System.out.println("Errore nella query: "+ex);
            throw new DAOException(ex.toString());
        } 
        return elementi;
    }
    
    public Object trovaElementoByIdInTransaction(Integer id, BeanBase bean) throws DAOException{
        Session session = DAOConnectionSingleton.newSession();
        Object objReturn = null;
        try{
            objReturn = (Object)session.load(bean.getClass(), id);
        }catch(Exception ex){
            System.out.println("Errore nella load: "+ex);
            throw new DAOException(ex.toString());
        } 
        return objReturn;
    }
    
    
    
}
